package com.itheima.tlias08_14.controller;


import com.itheima.tlias08_14.annotation.Log;
import com.itheima.tlias08_14.dto.StatisticEmpGenderDTO;
import com.itheima.tlias08_14.entity.Department;
import com.itheima.tlias08_14.dto.SelectDeptDTO;
import com.itheima.tlias08_14.entity.DepartmentDTO;
import com.itheima.tlias08_14.mapper.DepartmentMapper;
import com.itheima.tlias08_14.service.impl.DeptServiceImp;
import com.itheima.tlias08_14.utils.ResultUtil;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

@Slf4j
@RestController
//将该类注释为Controller
//@RequestMapping("/depts")
//RequestMapping 用于方法和类上
//1、作用是将请求路径映射到控制器方法上，统一方法中路径相同的请求
//2、可以定义请求的方式通过Method 属性指定请求的方式
//RequestMapping(method = RequestMethod.POST)
public class DeptController {

//    @Autowired
//    //获取执行SQL的Mapper对象
//    private DepartmentMapper mapper;

    @Autowired
    private DeptServiceImp deptService;

    @GetMapping("/depts")
    public ResultUtil selectAll() {
        ArrayList<SelectDeptDTO> departments = deptService.selectAllDTO();
//        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy年MM月dd日 HH时mm分ss秒");
//        //将Department对象转换为SelectDeptDTO对象
//        List<SelectDeptDTO> list = departments.stream().map(d -> {
//            SelectDeptDTO dto = new SelectDeptDTO();
//            dto.setId(d.getId());
//            dto.setName(d.getName());
//            dto.setUpdateTime(d.getUpdateTime().format(formatter));
//            return dto;
//        }).toList();
        HashMap<String, Object> map = new HashMap<>();
        map.put("code", 1);
        map.put("msg", "查询成功");
        map.put("data", departments);
        return !departments.isEmpty() ? ResultUtil.success(departments) : ResultUtil.failure();
    }

    /**
     * get/delete 请求方式 将请求值写在 url 后面用?连接
     * post/put 请求方式 将请求值写在请求体中
     * @param
     * @return
     * @RequestBody 将请求体中的数据封装到对象中
     */
    @PostMapping("/depts")
    //post 请求的两种写法
    // PostMapping -> RequestMapping(method = RequestMethod.POST)
//    @RequestMapping(method = RequestMethod.POST,value = "/depts")
    //@RequestBody 注解用于接收前端传递的json数据，并将数据转为目标对象
    @Log
    public ResultUtil insert(@RequestBody Department idp) {
        log.info("读取到：{}", idp.getName());

        return  deptService.insertDept
                (new Department(null, idp.getName(), LocalDateTime.now(), LocalDateTime.now()));
    }


    /**
     * @RequestParam 注解：获取url后面拼接的键值对的值
     * required  可以指定某些键的是否为必须性质
     * defaultValue  给定某些键的默认值
     */
    @DeleteMapping("/depts")
    //无注解就是获取 ? 后面拼接的键值对的值
    @Log
    public ResultUtil deleteDeptById(@RequestParam(value = "id", required = true) Integer id) {
       return deptService.deleteDeptById(id);
       // return i == 1 ? ResultUtil.success() : ResultUtil.failure();
    }

    @GetMapping("/depts/{id}")
    public ResultUtil selectDeptById(@PathVariable("id") Integer id, HttpServletRequest rqs) {
                                     //@PathVariable 获取Parameter的值 也就是获取 ? 后面拼接的键值对的值
        String requestURL = rqs.getRequestURI();
        StringBuffer requestURL1 = rqs.getRequestURL();
        log.info("请求路径为：{},{}", requestURL, requestURL1);

        SelectDeptDTO dept = deptService.selectDeptById(id);
        return dept!= null ? ResultUtil.success(dept) : ResultUtil.failure();
        //return dept != null ? ResultUtil.success(dept) : ResultUtil.failure();
    }

    @PutMapping("/depts")
    @Log
    public ResultUtil updateDept(@RequestBody Department dept) {
        ResultUtil resultUtil = deptService.updateDept(dept);
        return resultUtil;
    }

    //批量删除  获取多个Parameter的值

    @GetMapping("/depts/batch")
    @Log
    public ResultUtil batchDelete(@RequestParam("ids") List<Integer> ids) {
        log.info("批量删除的id为：{}", ids);
        int i = deptService.batchDelete(ids);
        return i >= 1 ? ResultUtil.success() : ResultUtil.failure();
    }

    @PostMapping("/depts/batch")
    @Log
    public ResultUtil batchInset(@RequestBody List<DepartmentDTO> list) {
        log.info("添加的元素为：{}", list);
        int i = deptService.batchInset(list);
        return i >= 1 ? ResultUtil.success() : ResultUtil.failure();
    }

    @Resource
    DepartmentMapper mapper;
    @GetMapping("/test")
    public ResultUtil test() {
        List<StatisticEmpGenderDTO> statisticEmpGenderDTOS = mapper.countDeptEmployee();
        return ResultUtil.success(statisticEmpGenderDTOS);
    }


}

